package com.dsj.estore.dao;

import com.dsj.estore.entity.ProductEntity;
import com.dsj.estore.util.DBContnection;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class ProductDao {
    private Connection connection;

    public ProductDao() {
        DBContnection dbContnection = new DBContnection();
        connection = dbContnection.connection;
    }


    //查询所有的商品信息
    public List<ProductEntity> selectAllProduct() {
        String sql = "SELECT picture, name, market_price, description FROM tb_product ORDER BY id DESC";
        Statement st = null;
        List<ProductEntity> list = new ArrayList();
        try {
            st = connection.createStatement();
            ResultSet rs = st.executeQuery(sql);
            //将结果集对象转为ArrayList，以数组的形式提交给前端处理
            while(rs.next()) {
                ProductEntity e = new ProductEntity();
                e.setPicture(rs.getString("picture"));
                e.setName(rs.getString("name"));
                e.setMarketPrice(rs.getDouble("market_price"));
                e.setDescription(rs.getString("description"));
                list.add(e);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }
}
